package com.example.ShoppingManagementSystem.control;


import com.example.ShoppingManagementSystem.entity.CartItem;
import com.example.ShoppingManagementSystem.entity.Customer;
import com.example.ShoppingManagementSystem.entity.PurchaseHistory;
import com.example.ShoppingManagementSystem.service.CustomerServicempl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/CustomerService")
@CrossOrigin
public class CustomerControl {

    private final CustomerServicempl customerService;
    @Autowired
    public CustomerControl(CustomerServicempl customerService) {
        this.customerService=customerService;
    }


    // 登录接口
    @PostMapping("/loginCustomer")
    public ResponseEntity<?> loginCustomer(@RequestParam String username, @RequestParam String password) {
        System.out.println("Attempting login with username: " + username);
        Customer customer = customerService.loginCustomer(username, password);
        if (customer != null) {
            return ResponseEntity.ok(customer);
        }
        return ResponseEntity.status(401).body("Invalid username or password");
    }

    // 注册接口
    @PostMapping("/registerCustomer")
    public ResponseEntity<?> registerCustomer(@RequestParam String username,
                                              @RequestParam String password,
                                              @RequestParam String phoneNumber,
                                              @RequestParam String email) {
        try {
            System.out.println("New customer registered: ");
            Customer newCustomer = customerService.registerCustomer(username, password, phoneNumber, email);
            System.out.println("here");
            return ResponseEntity.ok(newCustomer);
        } catch (RuntimeException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }

    // 更新密码接口
    @PutMapping("/updatePassword/{customerId}")
    public ResponseEntity<?> updatePassword(@PathVariable String customerId, @RequestParam String newPassword) {
        int id=Integer.parseInt(customerId);
        try {
            customerService.updatePassword(id, newPassword);
            return ResponseEntity.ok("Password updated successfully");
        } catch (RuntimeException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }

    // 重置密码接口
    @PostMapping("/resetPassword")
    public ResponseEntity<?> resetPassword(@RequestParam String username, @RequestParam String email) {
        boolean result = customerService.resetPassword(username, email);
        if (result) {
            return ResponseEntity.ok("Password reset successfully. Check your email.");
        }
        return ResponseEntity.badRequest().body("Invalid username or email");
    }

    // 添加到购物车接口
    @PostMapping("/addToCart")
    public ResponseEntity<?> addToCart(@RequestParam String ID, @RequestParam int productId, @RequestParam int quantity) {
        int Id = Integer.parseInt(ID);
        System.out.println(ID);
        customerService.addToCart(Id, productId, quantity);
        return ResponseEntity.ok("Item added to cart");
    }

    // 从购物车中移除接口
    @DeleteMapping("/removeFromCart")
    public ResponseEntity<?> removeFromCart(@RequestParam String customerID, @RequestParam int productId) {
        int customerId;
        try {
            customerId = Integer.parseInt(customerID);
        } catch (NumberFormatException e) {
            return ResponseEntity.badRequest().body("Invalid customer ID format.");
        }

        // 调用服务层的方法来处理从购物车中删除商品的逻辑
        customerService.removeFromCart(customerId, productId);

        return ResponseEntity.ok().body(Map.of("success", true, "message", "Item removed from cart"));
    }

    @PutMapping("/updateCartItemQuantity")
    public void updateCartItemQuantity(@RequestParam int customerId,
                                       @RequestParam int productId,
                                       @RequestParam int newQuantity) {
        customerService.updateCartItemQuantity(customerId, productId, newQuantity);
    }

    @GetMapping("/getAllPurchaseHistory")
    public List<PurchaseHistory> getAllPurchaseHistory(@RequestParam int customerId) {
        return customerService.getAllPurchaseHistory(customerId);
    }

    @PostMapping("/pay")
    public void pay(@RequestParam int customerId,
                    @RequestParam int productId,
                    @RequestParam int quantity) {
        customerService.pay(customerId, productId, quantity);
    }

    @GetMapping("/getCart")
    public List<CartItem> checkout(@RequestParam String customerId) {
        int id=Integer.parseInt(customerId);
        System.out.println(id);
        return customerService.checkout(id);
    }

}
